﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic.BusinessEntity.Entities;
using UserInterface.ClientApplication.UIEntity;


namespace UserInterface.ClientApplication
{
	public partial class AccountEditForm : Form, IForm
	{
		AccountView account = new AccountView();
		private int ind = -1;

		public AccountEditForm()
		{
			InitializeComponent();
		}



		#region IForm Members

		public FormManager fm { get; set; }


		public DialogResult Show(FormManager fm)
		{
			this.fm = fm;
			return ShowDialog();
		}

		#endregion

		private void AccountEditForm_Load(object sender, EventArgs e)
		{
			try
			{
				ind = (int)fm.Param;
				if (ind != -1)
				{
					account = fm.GetAccount(ind);
				}
			}
			catch (Exception)
			{
			}
			finally
			{
				InitBinding();
				ClientComboBox.SelectedItem = ind == -1 ? null : account.Accountee;
			}
		}

		private void InitBinding()
		{
			NumberTextBox.DataBindings.Add("Text", account, "Number");
			ClientComboBox.DataSource = fm.GetAllClient();
			ClientComboBox.DisplayMember = "Name";
			ClientComboBox.ValueMember = "ID";
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if (ValidateChildren())
			{
				if(ind == -1)
				{
					account.Accountee = (ClientView)ClientComboBox.SelectedItem;
					fm.AddAccount(account);
				}
				else
				{
					account.Accountee = (ClientView)ClientComboBox.SelectedItem;
					fm.SaveState();
				}
				Close();
			}else
			{
				DialogResult = System.Windows.Forms.DialogResult.None;
			}
		}

		private void CancelButton_Click(object sender, EventArgs e)
		{
			
			Close();
		}

		
		private void NumberTextBox_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (string.IsNullOrEmpty(NumberTextBox.Text))
			{
				error = "Пожалуйста, введите номер счета";
				e.Cancel = true;
			}
			for (int i = 0; i < NumberTextBox.Text.Length; i++)
			{
				if(!char.IsDigit(NumberTextBox.Text[i]))
				{
					error = "В счете должны быть только цифры";
					e.Cancel = true;
				}
			}

			if(NumberTextBox.Text.Length != 13)
			{
				if (error != null)
				{
					error += "\nНомер должен состоять из 13 цифр";
					e.Cancel = true;
				}
				else
				{
					error = "Номер должен состоять из 13 цифр";
					e.Cancel = true;
				}
			}
			
			if(fm.IsExistAccount(account))
			{
				if(error != null)
				{
					error += "\nТакой номер счета уже существует";

				}
				else
				{
					error = "Такой номер счета уже существует";
				}
				e.Cancel = true;
			}
				errorProvider.SetError((Control)sender, error);
		}

		private void ClientComboBox_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if(ClientComboBox.SelectedIndex == -1)
			{
				error = "Пожалуйста, выберите контрагента";
				e.Cancel = true;
			}
			errorProvider.SetError((Control)sender, error);

		}

		private void AccountEditForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			try
			{
				if (DialogResult != System.Windows.Forms.DialogResult.OK)
				{
					fm.LoadState();
				}
			}
			catch (Exception ex) { }
		}
	}
}
